
#include <Arduino.h>
#include "wifi_conn.h"
#include "web_server.h"
#include "ntp_client.h"
#include "eeprom_.h"
#include "blinker_.h"
#include "websocket_server.h"
#include "bemfa.h"

int wifi_mode_is_ap = true;
int blinker_enable = false;
int bemfa_enable = false;
int control_mode = 0;
int relay_io;
int led_io;
int computer_state_io;
int computer_rst_io;
void rst()
{
  if (control_mode == 0)
  {
    digitalWrite(relay_io, 1);
    delay(1000);
    digitalWrite(relay_io, 0);
    // Serial.print("\n继电器重启");
  }
  else if (control_mode == 1)
  {
    digitalWrite(computer_rst_io, 1);
    digitalWrite(computer_rst_io, 0);
    delay(1000);
    digitalWrite(computer_rst_io, 1);
    // Serial.print("\n电脑重启");
  }
  // led打开
  digitalWrite(led_io, 0);
}
void open_relay()
{
  if (control_mode == 0)
  {
    digitalWrite(relay_io, 0);
    // Serial.print("\n继电器开");
  }
  else if (control_mode == 1)
  {
    // 如果是关闭状态
    String state = read_state();
    if (state.compareTo("1") == 0)
    {
      digitalWrite(relay_io, 1);
      digitalWrite(relay_io, 0);
      delay(1000);
      digitalWrite(relay_io, 1);
      // Serial.print("\n电脑开");
    }
  }
  // led打开
  digitalWrite(led_io, 0);
}
void close_relay()
{
  if (control_mode == 0)
  {
    digitalWrite(relay_io, 1);
    // Serial.print("\n继电器关");
  }
  else if (control_mode == 1)
  {
    // 如果是开启状态
    String state = read_state();
    if (state.compareTo("0") == 0)
    {
      digitalWrite(relay_io, 1);
      digitalWrite(relay_io, 0);
      delay(1000);
      digitalWrite(relay_io, 1);
      // Serial.print("\n电脑关");
    }
  }
  // led关闭
  digitalWrite(led_io, 1);
}
String read_state()
{
  String state_io;
  if (control_mode == 0)
  {
    state_io = String(digitalRead(relay_io));
  }
  else if (control_mode == 1) // 读取电脑HDD+状态 状态向反
  {
    if (digitalRead(computer_state_io) == 0)
    {
      state_io = "1";
    }
    else
    {
      state_io = "0";
    }
  }
  return state_io;
}
void setup()
{
  Serial.begin(115200);
  eeprom_read();
  relay_io = eeprom_get_string(RELAY_IO_INDEX).toInt();
  led_io = eeprom_get_string(LED_IO_INDEX).toInt();
  computer_state_io = eeprom_get_string(COMPUTER_STATE_IO_INDEX).toInt();
  computer_rst_io = eeprom_get_string(COMPUTER_RST_IO_INDEX).toInt();
  control_mode = eeprom_get_string(CONTROL_MODE_INDEX).toInt();
  // io
  pinMode(relay_io, OUTPUT);
  pinMode(led_io, OUTPUT);
  pinMode(computer_state_io, INPUT);
  pinMode(computer_rst_io, OUTPUT);
  digitalWrite(relay_io, 1);
  digitalWrite(led_io, 1);
  digitalWrite(computer_rst_io, 1);
  // 连接wifi
  wifi_conn_setup();
  // blinker bemfa 启用判断
  String blinker_auth = eeprom_get_string(BLINKER_AUTH_INDEX);
  String blinker_enable_str = eeprom_get_string(BLINKER_ENABLE_INDEX);
  String bemfa_auth = eeprom_get_string(BEMFA_AUTH_INDEX);
  String bemfa_enable_str = eeprom_get_string(BEMFA_ENABLE_INDEX);
  if (!wifi_mode_is_ap && blinker_enable_str.compareTo("open") == 0)
  {
    blinker_enable = true;
    Serial.print("\n启用blinker  ");
    blinker_setup();
  }
  if (!wifi_mode_is_ap && bemfa_enable_str.compareTo("open") == 0)
  {
    bemfa_enable = true;
    Serial.print("\n启用巴法云  ");
    bemfa_setup();
  }

  web_server_setup();
  websocket_setup();
  ntp_client_setup();
}

void loop()
{
  if (!wifi_mode_is_ap && blinker_enable)
  {
    blinker_loop();
  }
  if (!wifi_mode_is_ap && bemfa_enable)
  {
    bemfa_loop();
  }
  web_server_loop();
  websocket_loop();
  ntp_client_loop();
}
